/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.cv4j.app.app;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;

import com.cv4j.app.ResourceTable;

/**
 * AbilitySlice基类
 *
 * @since 2021-05-17
 */
public class BaseAbilitySlice extends AbilitySlice {
    private Text title;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        int layout = getUIContentLayoutId();
        if (layout > 0) {
            ComponentContainer root = (ComponentContainer) LayoutScatter.getInstance(this).parse(layout, null, false);
            setUIContent(root);
        }
        title = (Text) findComponentById(ResourceTable.Id_title);
        if (title != null) {
            title.setText("< " + intent.getStringParam("Title"));
            title.setClickedListener(component -> {
                terminate();
            });
        }
        initComponents(intent);
    }

    /**
     * 获取标题栏标题
     *
     * @return 标题
     */
    public Text getTitle() {
        return title;
    }

    /**
     * 初始化
     *
     * @param intent intent
     */
    protected void initComponents(Intent intent) {
    }

    /**
     * 获取布局id
     *
     * @return 布局id
     */
    protected int getUIContentLayoutId() {
        return -1;
    }
}
